package com.marketingexpert.interceptor;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.marketingexpert.config.Configuration;
import com.marketingexpert.exception.MarketingExpertException;
import com.marketingexpert.service.snsmgnt.SnsService;
import com.marketingexpert.service.snsmgnt.SnsServiceFactory;
import com.marketingexpert.service.storemgnt.StoreService;
import com.marketingexpert.service.storemgnt.StoreServiceFactory;
import com.marketingexpert.util.Resources;
import com.marketingexpert.util.WebUtils;
import com.taobao.api.domain.Shop;

public class CustomTypeCheckorInterceptor extends HandlerInterceptorAdapter {

	private static Logger logger = Logger
			.getLogger(CustomTypeCheckorInterceptor.class);

	private String interceptResource;

	private SnsServiceFactory snsServiceFactory;

	private StoreServiceFactory storeServiceFactory;

	private Configuration configuration;

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {

		String reqPath = request.getServletPath();
		try {
			Pattern p = Pattern.compile(interceptResource);
			Matcher m = p.matcher(reqPath);

			if (!m.find()) {
				return true;
			}

//			String taobaoUserType = (String) request.getSession().getAttribute(
//					"taobaoUserType");

			boolean hasShop = (Boolean) request.getSession().getAttribute("hasShop");
//			hasShop = this.checkHasShop(taobaoUserType);
			if (hasShop) {
				return true;
			} else {
				throw new MarketingExpertException("shop null");
			}
		} catch (MarketingExpertException e) {
			request.setAttribute("errorTip", Resources.getString("ShopNeedTip"));
			request.getSession().getServletContext().getRequestDispatcher("/pages/error.jsp").forward(request, response);
			return false;
		}
	}

	private boolean checkHasShop(String taobaoUserType) {

		if (taobaoUserType.equalsIgnoreCase(configuration.getMallSeller())
				|| taobaoUserType.equalsIgnoreCase(configuration
						.getMarketSeller())) {
			return true;
		} else if (taobaoUserType.equalsIgnoreCase(configuration.getBuyer())) {
			return false;
		} else {
			return false;
		}
	}

	public SnsServiceFactory getSnsServiceFactory() {
		return snsServiceFactory;
	}

	public void setSnsServiceFactory(SnsServiceFactory snsServiceFactory) {
		this.snsServiceFactory = snsServiceFactory;
	}

	public String getInterceptResource() {
		return interceptResource;
	}

	public void setInterceptResource(String interceptResource) {
		this.interceptResource = interceptResource;
	}

	public StoreServiceFactory getStoreServiceFactory() {
		return storeServiceFactory;
	}

	public void setStoreServiceFactory(StoreServiceFactory storeServiceFactory) {
		this.storeServiceFactory = storeServiceFactory;
	}

	public Configuration getConfiguration() {
		return configuration;
	}

	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}
}
